<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Comments extends AdminController {
	
	var $num_elements = 10;
		
	function Comments() {
		parent::AdminController ();
		
		$this->load->model ( "Node_Model" , "Nodes" );
		$this->load->model ( "Comment_Model" , "Comments" );
		
		$this->load->helper('language');
		$this->lang->load('admin');

		$this->load->helper('date');
		
		$this->load->library("CTypes");
		
		$num_elements = $this->settings->item("_admin_list_elements");
		if($num_elements != NULL){
			$this->num_elements = $num_elements;
		}
	}
	
	
	function index(){
		$this->load->library('pagination');
		
		$pag = $this->pagination->segment_value();
		if($pag == NULL){
			$pag = 0;			
		}
		
		$params = array(
			"status" 	=> "all"
		);
		
		if(isset($_GET['node'])){
			$params['node_id'] = $_GET['node'];
		}
		
		if(isset($_GET['user'])){
			$params['user'] = $_GET['user'];
		}
		
		if(isset($_GET['ip'])){
			$params['ip'] = $_GET['ip'];
		}
		
		if(isset($_GET['status'])){
			$params['status'] = $_GET['status'];
			$this->_addData("filter_status", $_GET['status']);
		}
		
		$total = $this->Comments->getCount($params);
		$comments = $this->Comments->get($params, $pag, $this->num_elements);
		
		$comments_process = array();
		foreach($comments as $comment){
			$nodes_comments = $this->Comments->getCount(array(
				"node_id" 	=> $comment->node_id
			));
			
			$comment->node_count = $nodes_comments;
			
			array_push($comments_process, $comment);
		}

		$config['base_url'] = "/admin/comments/index/";
		$config['total_rows'] = $total;
		$config['per_page'] = $this->num_elements;
		//$config['uri_segment'] = 4;
		
		$config['full_tag_open'] = '<div class="pagination">';
		$config['full_tag_close'] = '</div>';
		
		$config['num_tag_open'] = '<span class="number">';
		$config['num_tag_close'] = '</span>';
		
		$config['cur_tag_open'] = '<span class="number current">';
		$config['cur_tag_close'] = '</span>';
		
		$config['first_link'] = 'Inicio';
		$config['last_link'] = 'Último';
		$config['prev_link'] = '&lt; Ant';
		$config['next_link'] = 'Sig &gt;';

		$this->pagination->initialize($config);
		
		$this->_addData("comments", $comments);
		$this->_addData("total_comments", $total);
		
		
		$params['status'] = "all";		
		$this->_addData("comments_all", $this->Comments->getCount($params));
		
		$params['status'] = "pending";
		$this->_addData("comments_pending", $this->Comments->getCount($params));
		
		$params['status'] = "approved";
		$this->_addData("comments_approved", $this->Comments->getCount($params));
		
		$params['status'] = "denied";
		$this->_addData("comments_denied", $this->Comments->getCount($params));
		
		$params['status'] = "hidden";
		$this->_addData("comments_hidden", $this->Comments->getCount($params));
		
		$params['status'] = "spam";
		$this->_addData("comments_spam", $this->Comments->getCount($params));
		

		$this->_render();
	}
	
	function delete($id = NULL) {
		if($id != null){
			$this->Comments->delete($id);
			$this->Comments->update_comment_count($id);
			$this->_addFlashSuccess("Comentario eliminado correctamente");
		}
		
		redirect ( $this->session->userdata("referrer"), 'refresh' );
	}
	
	function edit($id = NULL) {
		$this->_setPageView("comments/form");	
		
		if($id != NULL){
			$this->_addData("id", $id);
			
			$this->load->model ( "Comment_Model" , "Comments" );
			$comment = $this->Comments->get(array("id" => $id, "status" => "all"));
			
			if($comment != NULL){
			
				$this->form_validation->set_rules('data', 'Comentario', 'required');
				
				if($comment->user_id == NULL || $comment->user_id == -1){
					$this->form_validation->set_rules('name', 'Nombre', 'required|trim');
					$this->form_validation->set_rules('email', 'Email', 'trim|valid_email');
					$this->form_validation->set_rules('url', 'Url', 'trim');
				}
		
				if ($this->form_validation->run() == TRUE){
					
					$fields = array(
						'id' 	=> $id,
						'data'	=> $this->input->post("data")
					);
					
					if($comment->user_id == NULL || $comment->user_id == -1){
						$fields['name'] = $this->input->post("name");
						$fields['email'] = $this->input->post("email");
						$fields['url'] = $this->input->post("url");
					}
					
					$this->Comments->update($fields);
					
					$this->Comments->update_comment_count($comment->node_id);
					
					$this->_addFlashSuccess("Comentario modificado correctamente");
					
					$referrer = $this->session->userdata("referrer");
					if($referrer != NULL && $referrer != ""){
						redirect ($referrer, "refresh");
					} else{
						redirect("admin/comments", 'refresh');
					}
					
				} else{				
					$this->_addData("comment", $comment);
					
					$this->_render();
				}
			}
		}
		
	}
	
	function change_status($comment_id = NULL, $status = NULL){
		if($comment_id != NULL && $status != NULL){
			if($status == "approved" || $status == "denied" || $status == "spam" || $status == "hidden"){
				$comment = $this->Comments->get(array("id" => $comment_id, "status" => "all"));
				
				if($comment != NULL){
					$update_comment = $this->Comments->update(array("id" => $comment_id, "status" => $status));
					
					if(!$update_comment){
						//No actualizado correctamente
						$this->_addFlashError("Ha ocurrido errores a la hora de cambiar el estado");
					} else{
						
						//Actualizamos el numero de comentario en el nodo
						$this->Comments->update_comment_count($comment->node_id);
						
						$this->_addFlashSuccess("Comentario modificado correctamente");
					}
				} else{
					//El comentario no existe
					$this->_addFlashError("El comentario no existe");
				}
				
			} else {
				//Status no valido
				$this->_addFlashError("El estado para el comentario no es valido");
			}
		} else{
			//comentario_id o status no tienen valor
			$this->_addFlashError("Los parametros para actualizar el comentario no son validos");
		}
		
		$referrer = $this->session->userdata("referrer");
		
		if($referrer != NULL && $referrer != ""){
			redirect ($referrer, "refresh");
		} else{
			redirect("admin/comments", 'refresh');
		}
		
	}
	
	function response($comment_id = NULL){
		if($comment_id != NULL){
			$comment = $this->Comments->get(array("id" => $comment_id, "status" => "all"));
			
			if($comment != NULL){
				
				$this->form_validation->set_rules('data', 'Comentario', 'required');				
		
				if ($this->form_validation->run() == TRUE){
					
					$user_profile = $this->redux_auth->profile();
					
					$fields = array(
						"parent_id" => $comment_id,
						"user_id"	=> $user_profile->id,
						"status"	=> "approved",
						"node_id"	=> $comment->node_id,
						"data"		=> $this->input->post("data")
					);
					
					$id = $this->Comments->create($fields);
					
					//Actualizamos el numero de comentario en el nodo
					$this->Comments->update_comment_count($comment->node_id);
					
					$this->_addFlashSuccess("Comentario responido correctamente");
					
					$referrer = $this->session->userdata("referrer");
					if($referrer != NULL && $referrer != ""){
						redirect ($referrer, "refresh");
					} else{
						redirect("admin/comments", 'refresh');
					}
					
				} else{				
					$this->_addData("comment", $comment);
					
					$this->_render();
				}
				
			} else{
				$this->_addFlashError("El comentario no existe");
			}
		} else{
			$this->_addFlashError("El comentario no existe");
		}
	}
	
}
?>
